﻿using CommonFunction;
using MyWPF.Models.UserManagement;
using MyWPF.StaticData;
using MyWPF.Views.Others;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.User
{
    public class VM_UserWindow_ModifyUser : BaseViewModel
    {
        public VM_UserWindow_ModifyUser() 
        {
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _modifyUser = new DelegateCommand<object>(_ModifyUser);

            ItemsList = new List<Models.UserManagement.UserPower>
            {
                Models.UserManagement.UserPower.Tourist,
                Models.UserManagement.UserPower.Operator,
                Models.UserManagement.UserPower.Engineer,
                Models.UserManagement.UserPower.Administrator
            };
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.userWindow_ModifyUser.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.userWindow_ModifyUser.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.userWindow_ModifyUser.DragMove();
        }

        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion


        #region 属性绑定：用户输入的参数
        private string _userName;
        /// <summary>
        /// 用户输入的用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { SetProperty(ref _userName, value); }
        }
        private string _oldPassWord;
        /// <summary>
        /// 用户输入的老密码
        /// </summary>
        public string OldPassWord
        {
            get { return _oldPassWord; }
            set { SetProperty(ref _oldPassWord, value); }
        }
        private string _passWord1;
        /// <summary>
        /// 用户输入的新密码
        /// </summary>
        public string PassWord1
        {
            get { return _passWord1; }
            set { SetProperty(ref _passWord1, value); }
        }
        private string _passWord2;
        /// <summary>
        /// 用户输入的确认密码
        /// </summary>
        public string PassWord2
        {
            get { return _passWord2; }
            set { SetProperty(ref _passWord2, value); }
        }
        /// <summary>
        /// 权限列表
        /// </summary>
        public List<Models.UserManagement.UserPower> ItemsList
        {
            get { return _itemsList; }
            set { SetProperty(ref _itemsList, value); }
        }
        private List<Models.UserManagement.UserPower> _itemsList;
        /// <summary>
        /// 用户选择的权限，游客=1000，操作员=2000，工程师=3000，管理员=4000
        /// </summary>
        public UserPower UserSelected
        {
            get { return _userSelected; }
            set
            {
                SetProperty(ref _userSelected, value);
                switch (value)
                {
                    case Models.UserManagement.UserPower.None:
                        UserPower = 0;
                        break;
                    case Models.UserManagement.UserPower.Tourist:
                        UserPower = 1000;
                        break;
                    case Models.UserManagement.UserPower.Operator:
                        UserPower = 2000;
                        break;
                    case Models.UserManagement.UserPower.Engineer:
                        UserPower = 3000;
                        break;
                    case Models.UserManagement.UserPower.Administrator:
                        UserPower = 4000;
                        break;
                }
            }
        }
        private UserPower _userSelected;
        private int UserPower;
        /// <summary>
        /// 用户输入的备注
        /// </summary>
        public string Remarks
        {
            get { return _remarks; }
            set { SetProperty(ref _remarks, value); }
        }
        private string _remarks;
        #endregion
        
        #region 事件绑定：修改用户
        /// <summary>
        /// 修改用户
        /// </summary>
        private ICommand _modifyUser;
        public ICommand ModifyUser
        {
            get { return _modifyUser; }
            set
            {
                _modifyUser = value;
                OnPropertyChanged("ModifyUser");
            }
        }
        private void _ModifyUser(object obj)
        {
            UserContext UpdataUser = StaticModels.user.GetUserStruct_UserName(new UserContext
            {
                UserName = UserName
            })[0];

            //检测用户输入有没有问题
            string error = "";
            if (OldPassWord == null || OldPassWord == "" || PassWord1 == null || PassWord1 == "" || PassWord2 == null || PassWord2 == "")
            {
                error = "密码不能为空！\n";
            }
            if (PassWord1 != PassWord2)
            {
                error += "两次密码不一致！\n";
            }
            if (UserSelected == Models.UserManagement.UserPower.None || UserSelected == null)
            {
                error += "未选择任何权限！\n";
            }
            if (UserPower > StaticModels.user.User.PowerLevel)
            {
                error += "选择权限大于当前用户权限！\n";
            }
            if (UpdataUser.Password != OldPassWord)
            {
                error += "原密码错误！\n";
            }
            if (error.Count() > 0)
            {
                error = "输入有误，存在以下问题：\n" + error;
                Tips tips = new Tips(error);
                tips.ShowDialog();
                return;
            }


            //更新内容
            UpdataUser.Password = PassWord1;
            UpdataUser.Remarks = Remarks;
            UpdataUser.Power = UserSelected;
            StaticModels.user.ModifyUser(UpdataUser);


            //如果改的是当前用户权限，就更新当前用户权限
            if (StaticModels.user.User.UserName == UserName)
                StaticModels.user.Login(UpdataUser);

            //刷新用户列表
            StaticModels.vM_UserWindow.UserDataTable = StaticModels.user.GetUser_Level(StaticModels.user.User);

            //提示用户
            Tips tips1 = new Tips("修改成功！");
            StaticModels.userWindow_ModifyUser.Close();
            tips1.ShowDialog();

            //将界面用户输入清空
            OldPassWord = "";
            PassWord1 = "";
            PassWord2 = "";
            UserSelected = Models.UserManagement.UserPower.None;
            Remarks = "";


        }
        #endregion
    }
}
